package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SnackbarUtils.dismiss
import com.github.gzuliyujiang.wheelpicker.widget.LinkageWheelLayout
import com.lihang.ShadowLayout
import com.lv.common.im.OnPositionMoreCancle
import com.lv.common.imp.OnPositionMore
import com.lv.common.imp.OnPositionTListener
import com.lv.common.network.model.DataResult
import com.lv.common.utils.Constants.pageSize
import com.lv.common.utils.MyUtils
import com.lv.common.utils.showToast
import com.lv.service.R
import com.lv.service.adapter.CardCategoryImageBottomAdapter
import com.lv.service.bean.CategoryPeriodsDataBean
import com.lv.service.bean.TelSecondCategory
import com.lv.service.model.ServiceViewModel
import com.lv.service.net.CategoryPeriodsBody
import com.lv.service.network.BaseYgxRsp
import com.lv.service.network.onBizError
import com.lv.service.network.onBizOK
import com.lv.service.network.onSuccess
import com.lv.service.room.category.CategoryBean
import com.lv.service.utils.YgxToolsKt
import com.lv.service.weidget.picker.ThirdLikeProvider
import com.lxj.xpopup.core.BottomPopupView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class PeroidProvinceSelectedPopupView(
    private val mContext: Context,
    private val mAllBeans: List<CategoryBean>?,
    private var mWheelPosition1: Int = 0,
    private var mWheelPosition2: Int = 0,
//    private val mBeans: List<CategoryPeriodsBody>?,//已经存在的期数分类信息
    private var onPopSureListener: OnPositionMore<List<CategoryBean>>?
) :
    BottomPopupView(mContext), OnClickListener {
    private val MAXA_SELECTED = 5
    private var maxSelected = 1
    private var mPosition = 0
    private val name = ""
    private var mTab = 0

    //标记进入时的省份、期数位置，用来判断是否修改了省份期数
    private var mWheelTemPosition1: Int = 0
    private var mWheelTemPosition2: Int = 0
    private var mProvince = ""
    private var mPeriod = ""
    private var viewTop: View? = null

    private var viewBottom: View? = null
    private var tvSubmit: TextView? = null
    private var mViewShadow: ShadowLayout? = null
    private var wheelLinkage: LinkageWheelLayout? = null
    private val myScope: CoroutineScope = CoroutineScope(SupervisorJob())

    override fun getImplLayoutId(): Int {
        return R.layout.peroid_province_bottom_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewTop = findViewById<View>(R.id.view_top)
        viewBottom = findViewById<View>(R.id.view_bottom)
        tvSubmit = findViewById<TextView>(R.id.tv_submit)
        tvSubmit?.setOnClickListener(this)

        mViewShadow = findViewById<ShadowLayout>(R.id.view_shadow)
        wheelLinkage = findViewById<LinkageWheelLayout>(R.id.wheel_linkage)

        findViewById<ImageView>(R.id.iv_close).setOnClickListener { dismiss() }


        initView()
        initEvnet()
    }


    override fun onShow() {
        super.onShow()
        initData()

    }

    private fun initView() {
        mWheelTemPosition1 = mWheelPosition1
        mWheelTemPosition2 = mWheelPosition2
        mProvince = mAllBeans?.get(mWheelPosition1)?.name ?: ""
        mPeriod = YgxToolsKt.getPeriod(
            mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(
                mWheelPosition2
            )?.name ?: ""
        )
        val thirdLikeProvider = ThirdLikeProvider(mAllBeans)
        wheelLinkage?.setData(thirdLikeProvider)

        wheelLinkage?.setDefaultValue(mWheelPosition1, mWheelPosition2, 0)
        wheelLinkage?.thirdWheelView?.visibility = View.GONE
        initData()

        initEnable()
    }


    private fun initData() {

    }

    private fun initEnable() {
//        val mSelectedCategorys =
//            mCategorys.filter { item -> (!item.isItemHeader) && item.isSelcted }

    }


    private fun initEvnet() {

        viewTop?.setOnClickListener {
            dismiss()
        }

        wheelLinkage?.setOnLinkageSelectedListener { first, second, third ->
            mWheelPosition1 = wheelLinkage?.firstWheelView?.currentPosition ?: 0
            mWheelPosition2 = wheelLinkage?.secondWheelView?.currentPosition ?: 0
            mProvince = mAllBeans?.get(mWheelPosition1)?.name ?: ""
            mPeriod = YgxToolsKt.getPeriod(
                mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(
                    mWheelPosition2
                )?.name ?: ""
            )
//            mWheelPosition3 = wheelLinkage?.thirdWheelView?.currentPosition ?: 0
//            mBinding.tvSearchCategory.text =
//                "${mAllBeans!![mWheelPosition1].name}-${mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].name}"
        }


    }


    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }


    override fun onClick(v: View?) {
        when (v?.id) {

            R.id.tv_submit -> {
                onPopSureListener?.onClick(
                    mWheelPosition1,
                    mWheelPosition2,
                    data = mAllBeans
                )
                dismiss()
            }
        }
    }


    private fun initEditorProvince(mBeans: List<CategoryPeriodsBody>, toast: String) {
        var isAdded = false
        for (i in mBeans.indices) {
            val categoryPeriodsBody = mBeans[i]
            val province = categoryPeriodsBody.province
            val periods = YgxToolsKt.getPeriod(categoryPeriodsBody.periods)
            if (
                (province.contains(mProvince) || mProvince.contains(province))
                && (periods.contains(mPeriod) || mPeriod.contains(periods))
            ) {
                showToast(toast)
                isAdded = true
                break
            }
        }

    }

}